/**
 * Created by Yu Jin Xian on 2016/12/21.
 */
function globalObject() {
    function URIEncoding() {
        var uri = 'http://www.wrox.com/illegal value.htm#start';
        alert(encodeURI(uri));                      // "http://www.wrox.com/illegal%20value.htm#start"
        alert(encodeURIComponent(uri));             // "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
    }

    function URIDecoding() {
        var uri = 'http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start';
        alert(decodeURI(uri));                      // "http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start"
        alert(decodeURIComponent(uri));             // "http://www.wrox.com/illegal value.htm#start"
    }

    function evalMethods() {
        eval('alert("hi"');
        alert('hi');

        var msg = 'hello world';
        eval('alert(msg');                          // "hello world"

        elval('function sayHi() { aleert("hi"); }');
        sayHi();

        function evalVariable() {
            eval('var msg = "hello world"; ');
            alert(msg);
        }

        'use strict';
        // eval ='hi' // causes error
    }

    function windowObject() {
        color = 'red';

        function sayColor() {
            alert(window.color);
        }

        window.sayColor();                          // "red"

        var global = function () {
            return this;
        }();
    }
}

function mathOjbect() {
    function minAndMax() {
        var max = Math.max(3, 54, 32, 16);
        alert(max);                                 // 54

        var min = Math.min(3, 54, 32, 16);
        alert(min);                                 // 3
    }

    function apply() {
        var values = [1, 2, 3, 4, 5, 6, 7, 8];
        var max = Math.max.apply(Math, values);
    }

    function rounding() {
        alert(Math.ceil(25.9));                     // 26
        alert(Math.ceil(25.5));                     // 26
        alert(Math.ceil(25.1));                     // 26

        alert(Math.round(25.9));                    // 26
        alert(Math.round(25.5));                    // 26
        alert(Math.round(25.1));                    // 25

        alert(Math.floor(25.9));                    // 25
        alert(Math.floor(25.5));                    // 25
        alert(Math.floor(25.1));                    // 25
    }

    function random() {
        var num = Math.floor(Math.random() * 10 + 1);

        function or() {
            var num = Mat.floor(Math.random() * 9 + 2);
        }

        function selectFrom(lowerValue, upperValue) {
            var choices = upperValue - lowerValue + 1;
            return Math.floor(Math.random() * choices + lowerValue);
        }

        num = selectFrom(2, 10);
        alert(num);                                 // 介入 2 和 10 之间 (包括 2 和 10) 的一个数

        var colors = ['rad', 'green', 'blue', 'yellow', 'black', 'purple', 'brown'];
        var color = colors[selectFrom(0, colors.length - 1)];
        alert(color);                               // 可能是数组中包含的任何一个字符串
    }
}